package com.vma.interfaceShoes.controller;


import com.jfinal.core.Controller;
import com.sun.org.apache.xpath.internal.operations.Or;
import com.vma.interfaceShoes.util.PayResult;
import com.vma.interfaceShoes.util.ReturnsUtil;
import com.vma.interfaceShoes.util.XMLUtils;
import com.vma.neng.tools.wxPay.WxPay;
import com.vma.shoes.bizModel.model.OrderAction;
import com.vma.shoes.bizModel.model.OrderList;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.ServletInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Date;
import java.util.Map;


public class RecvWeiXinParamController  extends Controller{


	private static final Logger LOG = LoggerFactory.getLogger(RecvWeiXinParamController.class);

	
	/**
	 * 
	 * <xml><appid><![CDATA[wxf380b30bb430d138]]></appid><bank_type><![CDATA[CFT]]></bank_type><cash_fee><![CDATA[1]]></cash_fee><fee_type><![CDATA[CNY]]></fee_type><is_subscribe><![CDATA[Y]]></is_subscribe><mch_id><![CDATA[1240459502]]></mch_id><nonce_str><![CDATA[aZohipttHmAGpFTL1PmjvidEuq0jmNFz]]></nonce_str><openid><![CDATA[orS1LuKbo6nQW9S6cPvu9poDwogA]]></openid><out_trade_no><![CDATA[2015050813503167758]]></out_trade_no><result_code><![CDATA[SUCCESS]]></result_code><return_code><![CDATA[SUCCESS]]></return_code><sign><![CDATA[7325103D2A349CC89C3EE40E6063A7F8]]></sign><time_end><![CDATA[20150508135041]]></time_end><total_fee>1</total_fee><trade_type><![CDATA[JSAPI]]></trade_type><transaction_id><![CDATA[1009070562201505080116467741]]></transaction_id></xml>
		<xml>
		<appid><![CDATA[wxf380b30bb430d138]]></appid>
		<bank_type><![CDATA[CFT]]></bank_type>
		<cash_fee><![CDATA[1]]></cash_fee>
		<fee_type><![CDATA[CNY]]></fee_type>
		<is_subscribe><![CDATA[Y]]></is_subscribe>
		<mch_id><![CDATA[1240459502]]></mch_id>
		<nonce_str><![CDATA[aZohipttHmAGpFTL1PmjvidEuq0jmNFz]]></nonce_str>
		<openid><![CDATA[orS1LuKbo6nQW9S6cPvu9poDwogA]]></openid>
		<out_trade_no><![CDATA[2015050813503167758]]></out_trade_no>
		<result_code><![CDATA[SUCCESS]]></result_code>
		<return_code><![CDATA[SUCCESS]]></return_code>
		<sign><![CDATA[7325103D2A349CC89C3EE40E6063A7F8]]></sign>
		<time_end><![CDATA[20150508135041]]></time_end>
		<total_fee>1</total_fee>
		<trade_type><![CDATA[JSAPI]]></trade_type>
		<transaction_id><![CDATA[1009070562201505080116467741]]></transaction_id>
		</xml>
		
		30e351e70f31d74acaa6e611eb11cd8c

	 * @throws IOException 

	 */

	public void wxPayListener() throws IOException{
		 BufferedReader br = new BufferedReader(new InputStreamReader((ServletInputStream)getRequest().getInputStream()));
		 String line = null;
         StringBuilder sb = new StringBuilder();
         while((line = br.readLine())!=null){
             sb.append(line);
         }
         LOG.debug("wxPayListener==========="+sb.toString());
		 PayResult payResult= XMLUtils.getPayResult(sb.toString());
		 String olNbr = payResult.getOutTradeNo();
         if("SUCCESS".equals(payResult.getResultCode())){
			 OrderList orderList = OrderList.dao.findFirst("SELECT * FROM orderList WHERE orderNbr = ?",olNbr);
			 if ((orderList.getTotalCash()-orderList.getDiscount()) == Float.valueOf(payResult.getTotalFee())){
				 orderList.setStatus("2").update();//已付款
			 }
			 OrderAction orderAction = new OrderAction();
			 orderAction.setCreateTime(new Date()).setOperation("用户支付订单").setOrderId(orderList.getId()).setOpreationPeople("前台用户").save();
         }
		 System.out.println(getRequest().getRemoteHost());
		 getResponse().getWriter().write(" <xml> <return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>");
	}
	
	public void unifiedorder(){
		try{
			Integer orderId = getParaToInt("orderId");
			OrderList orderList = OrderList.dao.findById(orderId);
			Map map = WxPay.payInfo(orderList.getOrderNbr(),"一手鞋批发订单",String.valueOf(orderList.getTotalCash()*100),"http://120.77.86.178:41000/wxinpay/wxPayListener");
			renderJson(ReturnsUtil.returnSuccess(map));
		}catch (Exception e){
			e.printStackTrace();
			renderJson(ReturnsUtil.returnException(e.getMessage(),e));
		}
	}
}
